// JavaScript Document

var pageSize = 10;
var currentPage = 1;
var payStatus = 0;

$(document).ready(function()
{
console.log(getCookie('USER_TOKEN'));
	if(getCookie('USER_TOKEN') == null||  getCookie('USER_TOKEN')== '""'){
		$('#username').html("<strong>立即登录></strong>");
		$('#username').css("front-size","36px")
	}else{
		var phone = getCookie('USER_TOKEN');
		$.ajax({
	        url: '../mobile/getUserByToken',
	        type : "get",
	        dataType: "json",
	        async: true,
	        data:{
	        	token : getCookie('USER_TOKEN')  
	        },
	        success: function (result) {
	        	var phone = result.obj;
	        	var nPhone = phone.substr(0, 3) + '****' + phone.substring(7, 11);
	    		$('#username').html(nPhone);	        	
	        },
	        error: function () {
	        	
	        }        
	    });	
		
		$('#img_user img').attr("src", "../images/my_img_login.png");
	}
	$('.state_item')[0].click();
});

function userClick(){	
	
	if(getCookie('USER_TOKEN') == null||  getCookie('USER_TOKEN')== '""'){
		window.location.href = "../mobile/goLogin";
	}else{
		window.location.href = "../mobile/personalData?token="+getCookie('USER_TOKEN');
	}
}

$(window).load(function() 
{
    var _height = $('#img_bkg').height();
	
	var _top = 	(_height - 64) / 2;
	
	$('#user').css('top', _top + 'px');
});


function OrderStateClick(obj, type)
{
	$('.state_item').css('border-bottom', '0');	
	$('.state_item').css('color', '#a0a0a0');
	
	$(obj).css('border-bottom', '2px solid #f23e19');
	$(obj).css('color', '#f23e19');
	
	$('#order_list').find('tbody').html('');
	
	payStatus = type;
	if(getCookie('USER_TOKEN') == null||  getCookie('USER_TOKEN')== '""'){
		
		$("#orders").html('<a class="login_check_order">登录后查看订单</a>');
		
	}else{
		GetOrderInfo();
	}	
}

function GetOrderInfo()
{
	$.ajax({
        url: '../mobile/getMyOrder',
        type : "POST",
        dataType: "json",
       // async: true,
        data:{
        	token : getCookie('USER_TOKEN'),
			type : payStatus     
        },
        success: function (result) {
        	console.log(result);
        	
        		DisplayOrderInfo(result.obj);
			
        	        	
        },
        error: function () {
        	
        }        
    });			
}

function DisplayOrderInfo(data)
{
	var _html = '';
	var template = 
			'<div class="order_item">' +
            	'<div class="order_top">' +
                	'<a class="order_no">订单号：{0}</a><a class="vendout_state_{1}">{2}</a><a class="pay_state_{3}">{4}</a>' +
                '</div>' +
                '<div id="orderDetail{5}"></div>' +
                '<div class="order_bottom" onclick="orderDetail(\'{6}\',\'{5}\')" >' +
                	'<a class="detail" >订单详情</a><a class="total">共 {7} 件商品&nbsp;&nbsp;&nbsp;&nbsp;合计：￥{8}</a>' +
                '</div>' +
            '</div>';	
	
	var sendStatus = ["取消", "下单成功", "送货中", "订单完成"];//1.下单 2.确认3.送货4.已送到  0用户取消
	var payStatus = ["", "已支付","点击支付","异常订单"];
	var count = 0;
	var amount = 0;
	
	for (var i=0; i<data.length; i++)
	{		
		_html += template.format(data[i].orderNo, data[i].payStatus, payStatus[data[i].payStatus], data[i].sendStatus, sendStatus[data[i].sendStatus],data[i].orderId,data[i].orderNo, data[i].orderNumber, data[i].payPrice.toFixed(2));
		
	}
	
	$('#orders').html(_html);
	
}



function orderDetail(orderNo,orderId){
	
	
	if($('#orderDetail'+orderId+'').has("div").length){//元素是否存在
		
	    if($('#orderDetail'+orderId+'').css("display") =="block" ){
	    	$('#orderDetail'+orderId+'').hide();
	    }else{
	    	$('#orderDetail'+orderId+'').show();
	    }
	}else{
	    //元素不存在时的操作		
		$.ajax({
		    url: '../mobile/orderdeatial',
		    type : "POST",
		    dataType: "json",
		   // async: true,
		    data:{
		    	orderNo : orderNo + "" 	       
		    },
		    success: function (result) {
		    	console.log(result);
		    	if(result.success == 0) {
		    		
		    		var detailTemplate = 
		    			'<div  class="order_detail">' +
		    				'<img src="{0}">' +
		    				'<div class="order_text_left">' +
		    					'<a class="goods_name">{1}</a>' +
		    					'<a class="order_date">{2}</a>' +
		    				'</div>' +
		    				'<div class="order_text_right">' +
		    					'<a class="goods_price">￥ {3}</a>' +
		    					'<a class="goods_count">x{4}</a>' +
		    				'</div>' +
		    			'</div>';
		    		var _detail = '';
		    		for(var j=0; j<result.obj.length; j++){
		    			console.log(result.obj[j].createTime);
		    			_detail += detailTemplate.format(result.obj[j].dtlId, result.obj[j].commodityName, result.obj[j].createTime, result.obj[j].commodityAmount.toFixed(2), result.obj[j].commodityNums);
		    		}
		    		$('#orderDetail'+orderId+'').html(_detail);
		    		$('#orderDetail'+orderId+'').find("div").css("display","block"); //设置元素展示
				}		        	        	
		    },
		    error: function () {
		    	
		    }        
		});
	}		
}